package com.easycarebn.main.fragment;

import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.easycarebn.main.utils.ItemDr;
import com.easycarebn.main.utils.Utils;
import com.easycarebn.request.data.DoctorInfo;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.squareup.picasso.Picasso;
import com.timbacsi.main.R;

public class FragmentResultMap extends Fragment {
    public static final String FRAGMENT_TAG = "RESULT_MAP";

    private View mView;
    private static FragmentResultMap mFragmentResultMap = null;
    private FragmentActivity mFragmentActivity;
    private GoogleMap mMap;
    private HashMap<Marker, DoctorInfo> mMapItemDr;

    private Marker marker = null;

    public FragmentResultMap() {
        mMapItemDr = new HashMap<Marker, DoctorInfo>();
    }

    public static FragmentResultMap getInstance() {
        if (mFragmentResultMap == null) {
            mFragmentResultMap = new FragmentResultMap();
        }
        return mFragmentResultMap;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof FragmentActivity) {
            mFragmentActivity = (FragmentActivity) activity;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_result_map, null);
        return mView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        mMap = ((SupportMapFragment) mFragmentActivity.getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

        for (ItemDr item : FragmentSearchResult.newInstance("").getListDoctors()) {
            LatLng latlng = getlatLng(item.getInfo().getMapPositionDefault());
            Log.d("item.getInfo().getMapPositionDefault()", item.getInfo().getMapPositionDefault());
            if (latlng != null) {
                Marker marker = mMap.addMarker(new MarkerOptions().position(latlng).icon(
                        BitmapDescriptorFactory.fromResource(R.drawable.ic_location_map)));
                mMapItemDr.put(marker, item.getInfo());
            }
        }
        // Move the camera instantly to hamburg with a zoom of 15.
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(getlatLng(FragmentSearchResult.newInstance("").getListDoctors().get(0)
                .getInfo().getMapPositionDefault()), 15));

        // Zoom in, animating the camera.
        mMap.animateCamera(CameraUpdateFactory.zoomTo(12), 2000, null);

        mMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
                DoctorInfo info = mMapItemDr.get(marker);
                Bundle data = new Bundle();
                data.putInt(Utils.KEY_DOCTOR_ID, info.getId());
                data.putInt(Utils.KEY_SERVICE_TYPE, FragmentHome.newInstance().getSelectedSearchType());
                Utils.intentToDetailt(getActivity(), Utils.DOCTOR_SERVICE_DETAILT, data);

            }
        });

        mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());

        super.onActivityCreated(savedInstanceState);
    }

    private class CustomInfoWindowAdapter implements InfoWindowAdapter {

        private View view;

        public CustomInfoWindowAdapter() {
            view = getActivity().getLayoutInflater().inflate(R.layout.layout_doctor_info_map, null);
        }

        @Override
        public View getInfoContents(Marker marker) {

            FragmentResultMap.this.marker = marker;

            final DoctorInfo info = mMapItemDr.get(marker);

            final ImageView imgAvatar = (ImageView) view.findViewById(R.id.dr_avatar);

            TextView tvName = (TextView) view.findViewById(R.id.dr_name);
            TextView tvChuyenKhoa = (TextView) view.findViewById(R.id.dr_chuyen_khoa);
            TextView mTvDiaChi = (TextView) view.findViewById(R.id.dr_dia_chi);
            RatingBar mRatingBar = (RatingBar) view.findViewById(R.id.dr_star);

            Picasso.with(mFragmentActivity).load(info.getAvatar()).placeholder(R.drawable.avata).error(R.drawable.avata)
                    .into(imgAvatar);

            tvName.setText(info.getName());
            if (info.getSpeciality() != null) {
                tvChuyenKhoa.setText(String
                        .format(getResources().getString(R.string.chuyen_khoa_bs), info.getSpeciality().get(0)));
            }
//            else {
//                if (FragmentHome.newInstance().getSelectedSearchType() != SuggestNameType.HOME_SERVICE.getCode())
//                    tvChuyenKhoa.setText(info.getClinictype().get(0));
//            }
            mTvDiaChi.setText(info.getAddressDefault());
            mRatingBar.setRating(info.getRating());
            Log.d("getInfoWindow", "getInfoWindow1");
            return view;

        }

        @Override
        public View getInfoWindow(final Marker marker) {
            if (FragmentResultMap.this.marker != null && FragmentResultMap.this.marker.isInfoWindowShown()) {
                FragmentResultMap.this.marker.hideInfoWindow();
                FragmentResultMap.this.marker.showInfoWindow();
                Log.d("getInfoWindow", "getInfoWindow3");
            }

            Log.d("getInfoWindow", "getInfoWindow2");
            return null;

        }
    }

    public static LatLng getlatLng(String mapAdd) {
        System.out.println("LatLng: " + mapAdd);
        if (TextUtils.isEmpty(mapAdd)) {
            return null;
        }
        String[] latlng = mapAdd.split(",");
        if (latlng == null || latlng.length < 2) {
            return null;
        }
        float lat = Float.parseFloat(latlng[0]);
        float lng = Float.parseFloat(latlng[1]);
        return new LatLng(lat, lng);
    }
}
